package com.angus.modules.content.mapper;

import com.angus.modules.content.domain.ENContent;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface ENContentMapper extends BaseMapper<ENContent> {



    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cec.id,su.unit_num,su.course_code,su.is_pay,cec.group_id,cec.group_flag," +
            "            cec.en_word,cec.en_symbol,cec.en_translation," +
            "            cec.create_by,cec.create_time,cec.update_by,cec.update_time,cec.sort" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_en_content cec ON su.group_id = cec.group_id AND su.course_code = cec.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "          <if test=\"unitNum != null and unitNum > 0\">" +
            "              AND " +
            "              su.unit_num = #{unitNum}" +
            "          </if>" +
            "          AND " +
            "            su.del_flag = '0'" +
            "          AND " +
            "            cec.del_flag = '0'" +
            "          ORDER BY cec.sort asc" +
            "</script>")
    List<ENContent> getContentList(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum);


    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cec.id,su.unit_num,su.course_code,su.is_pay,cec.group_id,cec.group_flag," +
            "            cec.en_word,cec.en_symbol,cec.en_translation," +
            "            cec.create_by,cec.create_time,cec.update_by,cec.update_time" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_en_content cec ON su.group_id = cec.group_id AND su.course_code = cec.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "              AND su.unit_num = #{unitNum} " +
            "              AND su.group_id = #{groupId} " +
            "              AND su.del_flag = '0' " +
            "              AND cec.del_flag = '0' " +
            "</script>")
    List<ENContent> selectContentByGroupId(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum, @Param("groupId") Long groupId);

    @Select("<script>" +
            "SELECT DISTINCT " +
            "            cec.id,su.unit_num,su.course_code,su.is_pay,cec.group_id,cec.group_flag," +
            "            cec.en_word,cec.en_symbol,cec.en_translation," +
            "            cec.create_by,cec.create_time,cec.update_by,cec.update_time" +
            "             FROM " +
            "            sys_unit su " +
            "                JOIN course_en_content cec ON su.group_id = cec.group_id AND su.course_code = cec.course_code " +
            "        WHERE " +
            "            su.course_code = #{courseCode} " +
            "              AND su.unit_num = #{unitNum} " +
            "              AND cec.group_flag = #{groupFlag} " +
            "              AND su.del_flag = '0' " +
            "              AND cec.del_flag = '0' " +
            "</script>")
    ENContent selectContentByGroupFlag(@Param("courseCode") String courseCode, @Param("unitNum") Integer unitNum, @Param("groupFlag") String groupFlag);

    @Update("UPDATE course_en_content SET sort = id")
    int updateSortToId();
}
